//https://leetcode.cn/problems/maximum-erasure-value/submissions/558979436/

class Solution {
public:
    int maximumUniqueSubarray(vector<int>& v) {
        int l=0,r=0,n=v.size(),ans = 0, sum = 0, c=0;
        unordered_map<int, int> u;
        while (r < n) {
            u[v[r]]++;
            if (u[v[r]] == 1) c++;
            sum += v[r];

            while (c < r-l+1) {
                u[v[l]]--;
                sum -= v[l];
                if (!u[v[l++]]) c--;
            }
            ans = max(ans, sum);
            r++;
        }
        return ans;
    }
};